# Contributor: Carlo Landmeter <clandmeter@alpinelinux.org>
# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Carlo Landmeter <clandmeter@alpinelinux.org>
pkgname=font-noto
pkgver=24.7.1
pkgrel=0
pkgdesc="Noto font families for Latin, Cyrillic and Greek scripts"
url="https://github.com/notofonts/notofonts.github.io"
arch="noarch"
license="OFL-1.1"
depends="fontconfig $pkgname-common=$pkgver-r$pkgrel"
makedepends="ruby"
checkdepends="cmd:xmllint"
source="$pkgname-$pkgver.tar.gz::https://github.com/notofonts/notofonts.github.io/archive/refs/tags/noto-monthly-release-$pkgver.tar.gz
	45-noto-math.conf
	45-noto.xml.erb
	58-noto-math.conf
	58-noto.xml.erb
	noto-meta.json
	noto-meta
	"
builddir="$srcdir/notofonts.github.io-noto-monthly-release-$pkgver"

# List of subpackages generated by './noto-meta list-subpkgs'.
# NOTE: List is sorted by string length to prevent find doing partial matching.
_subpkgs="
	nyiakeng-puachue-hmong
	canadian-aboriginal
	indic-siyaq-numbers
	khitan-small-script
	hanifi-rohingya
	mayan-numerals
	gunjala-gondi
	masaram-gondi
	mende-kikakui
	nastaliq-urdu
	ottoman-siyaq
	cypro-minoan
	meetei-mayek
	naskh-arabic
	pahawh-hmong
	rashi-hebrew
	sora-sompeng
	syloti-nagri
	dives-akuru
	kufi-arabic
	medefaidrin
	nag-mundari
	nandinagari
	new-tai-lue
	pau-cin-hau
	signwriting
	warang-citi
	chorasmian
	devanagari
	historical
	old-uyghur
	saurashtra
	bassa-vah
	malayalam
	mongolian
	samaritan
	sundanese
	armenian
	balinese
	buginese
	cherokee
	duployan
	ethiopic
	fangsong
	georgian
	gujarati
	gurmukhi
	javanese
	kayah-li
	ol-chiki
	tagbanwa
	tifinagh
	vithkuqi
	znamenny
	bengali
	elbasan
	grantha
	hanunoo
	kannada
	makasar
	myanmar
	sharada
	sinhala
	soyombo
	symbols
	tibetan
	tirhuta
	arabic
	chakma
	coptic
	hebrew
	kaithi
	khojki
	lepcha
	rejang
	syriac
	tangsa
	telugu
	thaana
	wancho
	yezidi
	adlam
	bamum
	batak
	buhid
	khmer
	limbu
	music
	nushu
	oriya
	osage
	tamil
	ahom
	cham
	kawi
	lisu
	math
	miao
	modi
	newa
	test
	thai
	toto
	lao
	mro
	nko
	tai
	vai
	yi
	"
for _sub in $_subpkgs; do
	subpackages="$subpackages $pkgname-$_sub:_split"
done
subpackages="$subpackages
	$pkgname-extra
	$pkgname-common
	$pkgname-all:_all
	"

_fontsdir='usr/share/fonts/noto'

build() {
	noto_meta gen-font-confs conf.d/
}

check() {
	xmllint --quiet --loaddtd --valid --nonet \
		--path /usr/share/xml/fontconfig/ conf.d/*.conf >/dev/null
}

package() {
	depends="$depends
		$pkgname-math=$pkgver-r$pkgrel
		$pkgname-symbols=$pkgver-r$pkgrel
		"

	install -D -m644 fonts/*/hinted/ttf/*.ttf -t "$pkgdir/$_fontsdir"/
	install -D -m644 conf.d/*.conf -t "$pkgdir"/etc/fonts/conf.avail/

	cd "$pkgdir"

	mkdir -p etc/fonts/conf.d
	local f; for f in etc/fonts/conf.avail/*.conf; do
		ln -s ../conf.avail/${f##*/} "$pkgdir"/etc/fonts/conf.d/${f##*/}
	done
}

_split() {
	pkgdesc=$(noto_meta pkgdesc $subpkgname)
	provides=""
	replaces=""

	# For backward compatibility (Alpine <3.16).
	case "${subpkgname#font-noto-}" in
		kayah-li) replaces="$pkgname-kayahli";;
		ol-chiki) replaces="$pkgname-olchiki";;
		historical) replaces="$pkgname-egyptianhieroglyphs $pkgname-oldturkic";;
	esac
	local name
	[ "$replaces" ] && for name in $replaces; do
		provides="$provides $name=$pkgver-r$pkgrel"
	done
	replaces="$replaces $pkgname<0_git20211101-r1 $pkgname-extra<0_git20211101-r1"

	local font; for font in $(noto_meta font-basenames $subpkgname); do
		amove "$_fontsdir"/$font-*.ttf
	done

	local fontconf="${subpkgname#font-}.conf"
	amove etc/fonts/conf.avail/[0-9]*-$fontconf
	amove etc/fonts/conf.d/[0-9]*-$fontconf
}

extra() {
	pkgdesc="$pkgdesc (extra weights and variants)"
	depends="$pkgname=$pkgver-r$pkgrel"

	cd "$pkgdir"
	mkdir -p "$builddir"/.tmp

	local font weight
	for font in $(noto_meta font-basenames '@'); do
		for weight in Regular Italic Bold BoldItalic; do
			mv "$_fontsdir"/$font-$weight.ttf "$builddir"/.tmp/ 2>/dev/null || true
		done
		amove "$_fontsdir"/$font-*.ttf
	done

	# Take advantage of the fact that this split function runs as the last
	# one (we don't count -all) and check if there are any files not listed
	# in fonts-meta.json
	if [ "$(ls -A "$_fontsdir")" ]; then
		error "Unknown files found in /$_fontsdir (update fonts-meta.json):"
		ls -1 "$_fontsdir" >&2
		return 1
	fi

	mkdir -p "$_fontsdir"
	mv "$builddir"/.tmp/*.ttf "$_fontsdir"/
}

common() {
	pkgdesc="Common files for Noto font families"
	depends=""

	amove etc/fonts/conf.avail/58-noto.conf
	amove etc/fonts/conf.d/58-noto.conf
}

_all() {
	pkgdesc="Google Noto font families that aim to support all the world's languages (meta package)"
	depends="$pkgname=$pkgver-r$pkgrel
		$pkgname-extra=$pkgver-r$pkgrel
		"

	local subname; for subname in $_subpkgs; do
		depends="$depends $pkgname-$subname"
	done

	mkdir -p "$subpkgdir"
}

noto_meta() {
	"$srcdir"/noto-meta "$@"
}

sha512sums="
d9807011eef90b1ac21f56fc9518f3362e287fe3477534f66b370f897f95177d70b3e850bf76629d22a621aede61b6786e6cb0df1b8cabd6c18c70c4d06ef81c  font-noto-24.7.1.tar.gz
bbe805d212b0d46d4ce2d0fbc5a91d1f276a34f920b56ff6cb9474fdb3e1497bf9615cacfb9b69c04280ac4087c6f02a4fcb015b03c1ec0dd2d8180870811692  45-noto-math.conf
94c902aba178df855e388b1b2186866a0a28e6420f3f4ddbb798dfc7f551c13f93aa99c3ee7b112de218b817954b5f23c2cafdbd6a1828f2fec60ee61656e02a  45-noto.xml.erb
117830984239d10ce09bb79b7140b23810404ac13d44d1b3456587e34441273d9d4f4860fcabc19e8aec3ba3b60582c6b5411b28910e618fa2434968f83bd154  58-noto-math.conf
7024227b5b88f37bfff83792b36dbda66b27fdf472a0211c1b189a067d7cf7d406ad415e5a04ff1d7f067b9a4c9a97b275149062cb0ad65e7b0eed2835fa1c42  58-noto.xml.erb
972547e5617be83d746cfcb97e36b02fa814c1354dcc8fef388322e0c12138b77ba3586701e48b2a5659451894f4acd9770a44ac87e2f5217fb042f2042c767c  noto-meta.json
0e48ee2f69c4f2e4e5a5bb118d628a00c8a6479800e664c703a72b3e7a7c49c49f868a160a70faf242693e5daf797e97a2635b79be8470130ff3c2ed0a1a1b00  noto-meta
"
